package com.dpteam.controller;

import com.dpteam.bean.ProductInfo;
import com.dpteam.model.Category;
import com.dpteam.model.Provider;
import com.dpteam.service.ProductService;
import com.dpteam.util.InputCheckUtil;
import com.dpteam.util.Page;
import com.dpteam.util.PageFactory;
import com.dpteam.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xwh on 2016/1/13.
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;

    private static Logger logger = LoggerFactory.getLogger(GoodsController.class);

    /**
     * 添加商品界面
     */
    @RequestMapping("addProductHtml")
    public String addProductHtml(ModelMap map) {
        logger.info("addProductHtml");
        List<Category> categories = productService.findAllCategory();
        List<Provider> providers = productService.findAllProvider();
        map.put("categories", categories);
        map.put("providers", providers);
        return "addProduct";
    }

    /**
     * 添加一件商品
     */
    @RequestMapping("addProduct")
    public String addProduct(String barcode, String productName, int categoryId, float buyPrice, float setPrice, int providerId, String description) {
        logger.info("添加商品[barcode:{},productName:{},categoryId:{},buyPrice:{},setPrice:{},providerId:{},description:{}]", barcode, productName, categoryId, buyPrice, setPrice, providerId, description);
        // 校验
        boolean check = InputCheckUtil.isBarcode(barcode)
                && StringUtil.isNotEmpty(productName)
                && StringUtil.isNotEmpty(description);
        if (!check) {
            // 校验失败，重新刷新界面
            logger.info("校验失败");
            return "forward:/product/addProductHtml";
        }

        boolean result = productService.addProduct(barcode, productName, categoryId, buyPrice, setPrice, providerId, description);
        if (result) {
            //添加成功
            return "forward:/product/addProductHtml";
        } else {
            //添加失败
            return "error";
        }
    }

    /**
     * 修改商品
     */
    @RequestMapping("updateProduct")
    public String updateProduct(int productId, float productSetPrice) {
        logger.info("修改商品[id:{}, setPrice:{}]", productId, productSetPrice);
        boolean result = productService.updateProduct(productId, productSetPrice);
        if (result) {
            //修改成功
            return "forward:/product/findAllProduct";
        } else {
            //修改失败
            return "error";
        }
    }

    /**
     * 查询所有商品（分页）
     *
     * @param currentPage
     * @param map
     * @return
     */
    @RequestMapping("findAllProduct")
    public String findAllProduct(ModelMap map, @RequestParam(defaultValue = "1") int currentPage, @RequestParam(defaultValue = "10") int pageSize) {
        logger.info("findAllProduct");
        Page page = PageFactory.getPage(currentPage, pageSize);
        List<ProductInfo> products = productService.findAllProductPage(page);
        map.put("products", products);
        map.put("page", page);
        return "goods";
    }

    /**
     * 根据条形码查询商品
     *
     * @param barcode
     * @return
     */
    @RequestMapping("findProductByBarcode")
    public String findProductByBarcode(String barcode, ModelMap map) {
        logger.info("根据条形码查询商品[barcode:{}]", barcode);
        // 校验
        boolean check = InputCheckUtil.isBarcode(barcode);
        if (!check) {
            // 校验失败，重新刷新界面
            logger.info("校验失败");
            return "forward:/product/findAllProduct";
        }

        List<ProductInfo> products = new ArrayList<>();
        products.add(productService.findProductByBarcode(barcode));
        Page page = PageFactory.getPage();
        map.put("products", products);
        map.put("page", page);
        return "goods";
    }
}
